let app = getApp();
const QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
var demo = new QQMapWX({
  key: 'O76BZ-Y3JWQ-HAZ56-4JMND-JLKX6-XOBVR' // 必填
});

Page({
  data: {
    imgList: [],
    fileIDs: [],
    radio: '1',
    title: '',
    perface:false,
    detail:'',
    activeNames: [],
    local:'',
    taglist:[
      {color:'#AB4956',name:'西方'},
      {color:'green',name:'初学者'},
      {color:'#FFCA28',name:'春日颂'},
      {color:'pink',name:'甄嬛传'},
      {color:'blue',name:'长月烬明'},
      {color:'#8CA2B0',name:'声临其境'},
      {color:'#195D7A',name:'声临其境'},
      {color:'orange',name:'声临其境'}
    ],
    topic:[],
    show: false,
    showp: false,
    actions: [
      {
        name: '图片'
      },
      {
        name: '视频',
      },
    ],
    actionsp: [
      {
        name: '发表到论坛'
      },
      {
        name: '发表到剧本',
      },
      {
        name: '发表到合拍',
      },
    ]
  },
  onShow(){
    if(app.globalData.imgurl){
      this.setData({
        imgList: [app.globalData.imgurl],
        title:app.globalData.title,
        detail:app.globalData.outline,
        perface:true
      })
    }
  },
  onshowp(){
    this.setData({
      showp:!this.data.showp
    })
  },
  onClose() {
    this.setData({ show: !this.data.show });
  },
  onClosep() {
    this.setData({ showp: !this.data.showp });
  },

  onSelect(event) {
    console.log(event.detail);
    this.onClose()
    if(event.detail.name=='图片'){
      this.ChooseImage()
    }
    else{
      this.chooseVideo()
    }
  },
  onSelectp(event) {
    console.log(event.detail);
    this.onClosep()
    if(event.detail.name=='发表到论坛')
   this.publish()
   else if(event.detail.name=='发表到剧本'){
     this.pup() 
   }
   else{
     this.pupl() 
   }
  },
  chooseVideo: function() {
    var _this = this;
    wx.chooseVideo({
      success: function(res) {
        _this.setData({
          src: res.tempFilePath,
        })
        console.log(res)
      }
    })
  },
  onChanger(event) {
    console.log(event)
    this.setData({
      radio: event.detail
    });
  },

  onClickr(event) {
    const { name } = event.currentTarget.dataset;
    this.setData({
      radio: name
    });
  },

  onChange(event) {
    this.setData({
      activeNames: event.detail
    });
  },
  //获取输入内容
  getInput(event) {
    this.setData({ 
      detail: event.detail.value
    })
  },
  deltag(e){
    let tag=e.currentTarget.dataset.del
    let topic=this.data.topic.splice(tag,1)
    console.log(topic)
    this.setData({
      topic:this.data.topic
    })

  },
  local(){
    let that=this
    wx.getLocation({
      type: 'wgs84',
      success (res) {
        demo.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (res) {
            console.log(res.result);
            that.setData({
              local:res.result.address
            })
          },
          fail: function (res) {
            console.log(res);
          },
     })
     
  }
})}
,
  //获取输入标题
  titleBlur(event) {
    this.setData({
      title: event.detail.value
    })
  },
  //返回上一步
  back(){
    wx.navigateBack({
      delta: 1
    });
  },
  close(){
   this.setData({
     title:'',
     detail:'',
     imgList:[],
     src:false,
     perface:false
   })
   app.globalData.imgurl=false
  },
  title(e){
    let tag=e.currentTarget.dataset.tag
    let topic=this.data.topic.push(this.data.taglist[tag].name)
    console.log(this.data.topic)
    this.setData({
      topic:this.data.topic
    })

  },
  //选择图片
//可以用的 没有上传

  ChooseImage() {
    wx.chooseImage({
      count: 9,
      mediaType:['image'],
      sourceType: ['album'],
      sizeType: ['original', 'compressed'], 
      success: (res) => {
        if (this.data.imgList.length != 0) {
          this.setData({
            imgList: this.data.imgList.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList: res.tempFilePaths
          })
        }
        console.log(res)
      }
    })
  },
  //删除图片
  DeleteImg(e) {
    app.globalData.imgurl=false
    this.setData({
      perface:false
    })
    wx.showModal({
      title: '要删除这张照片吗？',
      content: '',
      cancelText: '取消',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          console.log("publish a")
          this.data.imgList.splice(e.currentTarget.dataset.e, 1);
          this.setData({
            imgList: this.data.imgList
          })
        }
      }
    })
  },
  DeleteV() {
    wx.showModal({
      title: '要删除这个视频吗？',
      content: '',
      cancelText: '取消',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          console.log("publish a")
          this.setData({
            src: false
          })
        }
      }
    })
  },
  // 查看大图可以用
  ViewImage: function(e) {
    var img = this.data.imgList;
    var itemIndex = e.currentTarget.id;
    wx.previewImage({
      current: img[itemIndex], 
      urls: img 
    })
  },

  onLoad(options) {
    if (options.fileName) {
      this.setData({
        src: `http://localhost:1234/creator/getVideo?name=${options.fileName}`
      })
    }

    // console.log (option.filePath);
  },

  publish() {
    let s='';
    this.data.topic.map(item=>{
      console.log(item)
      s+=','+item
    })
    console.log(s.substring(1))
    let mydata = {
      'title': this.data.title,
      'detail': this.data.detail,
      'userid':wx.getStorageSync('userid'),
      'picture':this.data.src?1:0,
      'much':this.data.imgList.length>1?true:false,
      'topic':s.substring(1),
      'person':this.data.radio,
      'location':this.data.local,
      'tu':this.data.imgList[0]?true:false
    }
    wx.request({
      url: 'http://localhost:1234/users/api/publish',
      method: 'POST',
      data: JSON.stringify(mydata),
      success: (res) => {
        console.log (res);
        wx.switchTab({
          url: '/pages/forum/forum',
        })
        if(res.data){
        if(this.data.src){
          if(this.data.src.includes('http://localhost:1234')){
            wx.request({
              url: 'http://localhost:1234/users/voice?'+this.data.src.split('?')[1]+'&data='+res.data,
              method: 'POST',
              success:(res)=>{
                wx.uploadFile({
                  filePath: this.data.imgList[0],
                  name: 'image',
                  url: 'http://localhost:1234/users/api/addimg?data='+res.data+'&img=1',
                  header: {//设置请求头
                    'content-Type': 'multipart/form-data',
                  },
                  success: (res) => {
                    console.log(res)
                    this.close()
              }})
              }
            })
          }
          else{
          wx.uploadFile({
            filePath: this.data.src,
            name: 'image',
            url: 'http://localhost:1234/users/api/addimg?data='+res.data,
            header: {//设置请求头
              'content-Type': 'multipart/form-data',
            },
            success: (res) => {
              console.log(res)
              wx.uploadFile({
                filePath: this.data.imgList[0],
                name: 'image',
                url: 'http://localhost:1234/users/api/addimg?data='+res.data+'&img=1',
                header: {//设置请求头
                  'content-Type': 'multipart/form-data',
                },
                success: (res) => {
                  console.log(res)
                  this.close()
            }})
          
        }})
      }
      }else{
      for(var i=0;i<this.data.imgList.length;i++){
        wx.uploadFile({
          filePath: this.data.imgList[i],
          name: 'image',
          url: 'http://localhost:1234/users/api/addimg?data='+res.data,
          header: {//设置请求头
            'content-Type': 'multipart/form-data',
          },
          success: (res) => {
            console.log(res)
            this.close()
            wx.switchTab({
              url: '/pages/forum/forum',
            })
      }
    }
    )}
  }
      wx.showToast({
       title: '成功',
       icon: 'success',
       duration: 2000//持续的时间
      })
  fail: (err) => {
    console.log (err);
  }
}
      }})},
  pup(){
        let mydata = {
          'name': this.data.title,
          'outline': this.data.detail,
          'userid':wx.getStorageSync('userid'),
          'type':0,
          'image':'a'
        }
        wx.request({
          url: 'http://localhost:1234/users/api/pup',
          method: 'POST',
          data: JSON.stringify(mydata),
          success: (res) => {
            console.log (res);
            if(res.data){
              wx.switchTab({
                url: '/pages/forum/forum',
              })
              wx.showToast({
                title: '成功',
                icon: 'success',
                duration: 2000//持续的时间
               })
                wx.uploadFile({
                  filePath: this.data.imgList[1],
                  name: 'image',
                  url: 'http://localhost:1234/users/api/addimgpup?data='+res.data+'&img=1',
                  header: {//设置请求头
                    'content-Type': 'multipart/form-data',
                  },
                  success: (res) => {
                    console.log(res)
                  
              }
            }
            )
                wx.uploadFile({
                  filePath: this.data.imgList[0],
                  name: 'image',
                  url: 'http://localhost:1234/users/api/addimgpup?data='+res.data+'&img=0',
                  header: {//设置请求头
                    'content-Type': 'multipart/form-data',
                  },
                  success: (res) => {
                    console.log(res)
                    this.close()
              }
            }
            )
      }
    }
  })
  },
  pupl(){
    let mydata = {
      'name': this.data.title,
      'outline': this.data.detail,
      'userid':wx.getStorageSync('userid'),
      'type':4,
      'image':this.data.src.split('name=')[1]
    }
    wx.request({
      url: 'http://localhost:1234/users/api/pup',
      method: 'POST',
      data: JSON.stringify(mydata),
      success: (res) => {
        console.log (res);
        wx.switchTab({
          url: '/pages/forum/forum',
        })
        if(res.data){
          wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 2000//持续的时间
           })
            wx.uploadFile({
              filePath: this.data.src,
              name: 'image',
              url: 'http://localhost:1234/users/api/addimgpup?data='+res.data+'&img=0',
              header: {//设置请求头
                'content-Type': 'multipart/form-data',
              },
              success: (res) => {
                console.log(res)
              
           
          }
        }
        )
            wx.uploadFile({
              filePath: this.data.imgList[0],
              name: 'image',
              url: 'http://localhost:1234/users/api/addimgpup?data='+res.data+'&img=1',
              header: {//设置请求头
                'content-Type': 'multipart/form-data',
              },
              success: (res) => {
                console.log(res)
                this.close()
          }
        }
        )
           
  }
}
})
  }
})
  


    



  